import Banner, { Channel } from "../Banner";
import { ColorHex, Constant } from "../Framework/Const/Constant";
import AudioManager from "../Framework/Managers/AudioManager";
import { DataManager } from "../Framework/Managers/DataManager";
import { EventManager, MyEvent } from "../Framework/Managers/EventManager";
import { UIManager } from "../Framework/Managers/UIManager";
import NodeUtil from "../Framework/Utils/NodeUtil";
import { ResourceUtil } from "../Framework/Utils/ResourceUtil";
import GameManager, { GameMode } from "../GameManager";
import PlayerController, { AniState } from "../PlayerController";
import TreasureBox from "../TreasureBox";

const { ccclass, property } = cc._decorator;

@ccclass
export default class StartPanel extends cc.Component {
    MoneyLabel: cc.Label = null;
    Player: cc.Node = null;
    SeasonLvLabel: cc.Label = null;
    playerCtrl: PlayerController = null;

    KeFuButton: cc.Node = null;
    MoreGameButton: cc.Node = null;
    QuitButton: cc.Node = null;

    protected onLoad(): void {
        GameManager.IsGameOver = false;

        this.MoneyLabel = NodeUtil.GetComponent("MoneyLabel", this.node, cc.Label);
        this.Player = NodeUtil.GetNode("Player", this.node);
        this.SeasonLvLabel = NodeUtil.GetComponent("SeasonLvLabel", this.node, cc.Label);
        this.KeFuButton = NodeUtil.GetNode("KeFuButton", this.node);
        this.MoreGameButton = NodeUtil.GetNode("MoreGameButton", this.node);
        this.QuitButton = NodeUtil.GetNode("QuitButton", this.node);

        this.RefreshMoney();
        AudioManager.PlayBGM(Constant.Audio.BG);
        Banner.Instance.JudgeChannel(this.SetButtonState.bind(this));
    }

    protected onDestroy(): void {
        AudioManager.StopBGM(Constant.Audio.BG);
    }

    bannerPopup = 0;

    protected start(): void {
        this.InitPlayer();
        if (Banner.NeedShowMoreAd) {
            Banner.Instance.ShowCustomAd();
        }
        this.SeasonLvLabel.string = `${DataManager.SeasonLv}`;

        Banner.Instance.ShowBannerAd();
        clearInterval(this.bannerPopup);
        this.bannerPopup = setInterval(() => Banner.Instance.ShowBannerAd(), 1000 * 30);
        // Banner.Instance.StartPopupAd();
    }

    RefreshMoney() {
        this.MoneyLabel.string = `${DataManager.Money}`;
    }

    RefreshEquip() {
        this.playerCtrl && this.playerCtrl.RefreshEquip();
    }

    InitPlayer() {
        ResourceUtil.LoadPrefab(`Player`).then((prefab: cc.Prefab) => {
            let player = cc.instantiate(prefab);
            player.setParent(this.Player);
            player.setPosition(cc.Vec2.ZERO);
            this.playerCtrl = player.getComponent(PlayerController);
            this.playerCtrl.hpBar.node.active = false;
            this.playerCtrl.PlayAni(AniState.Idle);
        })
    }

    OnButtonClick(event: cc.Event) {
        AudioManager.PlayAudio(Constant.Audio.ButtonClick);

        switch (event.target.name) {
            case "TeamButton":
                GameManager.GameMode = GameMode.TEAM;
                UIManager.Instance.ShowPanel(Constant.Panel.LoadingPanel, [`Lv_Team`]);
                break;
            case "ExcapeButton":
                GameManager.GameMode = GameMode.ESCAPE;
                UIManager.Instance.ShowPanel(Constant.Panel.SelectMapPanel);
                break;
            case "CangKuButton":
                if (Banner.NeedShowMoreAd) {
                    Banner.Instance.ShowCustomAd();
                }
                UIManager.Instance.ShowPanel(Constant.Panel.InventoryPanel, ["仓库", DataManager.PlayerItems]);
                break;
            case "ShiChangButton":
                if (Banner.NeedShowMoreAd) {
                    Banner.Instance.ShowCustomAd();
                }
                UIManager.Instance.ShowPanel(Constant.Panel.ShopPanel);
                break;
            case "DisagreeButton":
                break;
            case "AddMoneyButton":
                Banner.Instance.ShowVideoAd(() => {
                    AudioManager.PlayAudio(Constant.Audio.GetMoney);
                    DataManager.Money += 20000;
                });
                break;
            case "SkinShopButton":
                UIManager.Instance.ShowPanel(Constant.Panel.SkinShopPanel);
                break;
            case "SpecialSkinShopButton":
                UIManager.Instance.ShowPanel(Constant.Panel.SpecialSkinShopPanel);
                break;
            case "SeasonButton":
                UIManager.Instance.ShowPanel(Constant.Panel.SeasonPanel);
                break;
            case "RewardButton":
                if (Banner.NeedShowMoreAd) {
                    Banner.Instance.ShowCustomAd();
                }
                UIManager.Instance.ShowPanel(Constant.Panel.FreeRewardPanel);
                break;
            case "PrivacyButton":
                if (Banner.IS_ANDROID) {
                    Banner.Instance.AndroidPrivacy();
                } else {
                    UIManager.Instance.ShowPanel(Constant.Panel.PrivacyPanel, [false]);
                }
                break;

            case "KeFuButton":
                Banner.Instance.AndroidKeFu();
                break;

            case "MoreGameButton":
                Banner.Instance.AndroidMoreGame();
                break;

            case "QuitButton":
                Banner.Instance.AndroidLogout();
                break;
        }
    }

    SetButtonState(channel: Channel) {
        this.KeFuButton.active = channel == Channel.VivoBtn || channel == Channel.OppoBtn;
        this.MoreGameButton.active = channel == Channel.OppoBtn;
        this.QuitButton.active = channel == Channel.HuaweiBtn;
    }

    protected onEnable(): void {
        EventManager.on(MyEvent.RefreshEquip, this.RefreshEquip, this);
        EventManager.on(MyEvent.RefreshMoney, this.RefreshMoney, this);
    }

    protected onDisable(): void {
        EventManager.off(MyEvent.RefreshEquip, this.RefreshEquip, this);
        EventManager.off(MyEvent.RefreshMoney, this.RefreshMoney, this);
    }
}
